using System;
using System.Web;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI;
using Page=ASPNETVN.PORTAL.Components.UI.Page;

namespace ASPNETVN.PORTAL
{
    public partial class Default : Page
    {
        protected override void OnInit(EventArgs e)
        {
            // Control ID.
            if (Request.QueryString["cid"] != null)
            {
                ContentPane.Controls.Add(Page.LoadControl(string.Format("~/Controls/{0}.ascx", Request.QueryString["cid"])));
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            // Administration Control ID.
            else if (Request.QueryString["acid"] != null)
            {
                ContentPane.Controls.Add(Page.LoadControl(string.Format("~/Administration/{0}.ascx", Request.QueryString["acid"])));
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            else
            {
                // Obtain Portal from Current Context.
                Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];
                LeftPane.Visible = portal.ActiveTab.ShowLeftPane;
                RightPane.Visible = portal.ActiveTab.ShowRightPane;

                // Ensure that the visiting user has access to the current page.
                if (PortalSecurity.IsInRoles(portal.ActiveTab.AuthorizedRoles) == false)
                {
                    Response.Redirect("~/Errors/AccessDenied.aspx");
                }

                // Dynamically Populate the Left, Center and Right pane sections of the portal page.
                if (portal.ActiveTab.Modules.Count > 0)
                {
                    // Loop through each entry in the configuration system for this tab.
                    foreach (Module module in portal.ActiveTab.Modules)
                    {
                        Control parent;
                        switch (module.PaneName)
                        {
                            case "LeftPane":
                                parent = LeftPane;
                                break;
                            case "RightPane":
                                parent = RightPane;
                                break;
                            default:
                                parent = ContentPane;
                                break;
                        }

                        // If no caching is specified, create the user control instance and dynamically
                        // inject it into the page.  Otherwise, create a cached module instance that
                        // may or may not optionally inject the module into the tree

                        bool isViewableModule = PortalSecurity.HasViewAuthorization(module.ID) || PortalSecurity.IsInRole("1");
                        if (module.CacheTimeOut == 0)
                        {
                            // Check module visible.
                            if (isViewableModule)
                            {
                                PortalModuleControl portalModule = (PortalModuleControl)Page.LoadControl(module.SourceFile);
                                portalModule.Module = module;
                                parent.Controls.Add(portalModule);
                            }
                        }
                        else
                        {
                            CachedPortalModuleControl portalModule = new CachedPortalModuleControl();
                            portalModule.ModuleConfiguration = module;
                            parent.Controls.Add(portalModule);
                        }
                        parent.Visible = true;
                    }
                }
            }
            base.OnInit(e);
        }
    }
}